<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissFn(1, '0')">
            添加权限
          </el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="addPermissFn(2, row.id)"
              >
                添加权限点
              </el-button>
              <el-button
                size="small"
                type="text"
                @click="editPermissFn(row.id)"
              >
                查看
              </el-button>
              <el-button size="small" type="text" @click="delPermissFn(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog
      :visible="showDialog"
      :title="showDialogTitle"
      @close="closeFn()"
    >
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <el-button @click="closeFn()">取消</el-button>
          <el-button type="primary" @click="submitPermiss()">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqGetPermissionList,
  reqAddPermission,
  reqDelPermission,
  reqGetPermissionDetail,
  reqUpdatePermission
} from '@/api/permisson'
import { recursionDepartsFn } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [], // 权限列表
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    // 通过计算属性来计算 是编辑还是添加 的标题 通过id来判断
    showDialogTitle() {
      return this.formData.id ? '查看编辑权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取数据渲染
    async getPermissionList() {
      const { data } = await reqGetPermissionList() // 获取角色数据
      // 筛选一级二级的 数据 调用递归方法
      const list = recursionDepartsFn(data, '0')
      this.permissionList = list
    },
    //
    addPermissFn(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 电器了确定，添加权限方法
    async submitPermiss() {
      if (!this.formData.id) {
        await reqAddPermission(this.formData)
        this.$message.success('添加成功')
      } else {
        await reqUpdatePermission(this.formData)
        this.$message.success('编辑成功')
      }
      this.formData = {}
      this.getPermissionList()
      this.showDialog = false
    },
    // 删除权限点
    delPermissFn(id) {
      this.$confirm('此操作将永久删除该权限点吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await reqDelPermission(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      this.getPermissionList()
    },
    // 编辑权限
    async editPermissFn(id) {
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.showDialog = true
    },
    // g关闭
    closeFn() {
      this.formData = {}
      this.showDialog = false
    }
  }
}
</script>
